package com.monitor.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

import com.jfinal.log.Logger;
import com.monitor.service.MonitorServerSocket;

public class PropertyUtil {
	private Properties properties;
	private String fileName;
	private static Logger logger = Logger.getLogger(MonitorServerSocket.class);

	public PropertyUtil(String fileName,String encode){
		
		if(encode == null){
			encode = "UTF-8";
		}
		this.fileName = fileName;
		InputStream inputStream = null;
		try {
		inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);		// properties.load(Prop.class.getResourceAsStream(fileName));
		if (inputStream == null)
			throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
		properties = new Properties();
		
			properties.load(new InputStreamReader(inputStream, encode));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (inputStream != null) try {inputStream.close();} catch (IOException e) {e.printStackTrace();}
		}
	}
	
	public void store(){
		
		try {
			
			String path = URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource("").toString().substring(6), "UTF-8");
			logger.debug("输出文件路径"+path);
			FileOutputStream fileout = new FileOutputStream(path+fileName);
			properties.store(fileout, null);
			fileout.flush();
			fileout.close();
			logger.debug("保存配置文件完成");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String get(String key) {
		return properties.getProperty(key);
	}
	
	public String get(String key, String defaultValue) {
		String value = get(key);
		return (value != null) ? value : defaultValue;
	}
	
	public Integer getInt(String key) {
		String value = get(key);
		return (value != null) ? Integer.parseInt(value) : null;
	}
	
	public Integer getInt(String key, Integer defaultValue) {
		String value = get(key);
		return (value != null) ? Integer.parseInt(value) : defaultValue;
	}
	
	public Long getLong(String key) {
		String value = get(key);
		return (value != null) ? Long.parseLong(value) : null;
	}
	
	public Long getLong(String key, Long defaultValue) {
		String value = get(key);
		return (value != null) ? Long.parseLong(value) : defaultValue;
	}
	
	public Boolean getBoolean(String key) {
		String value = get(key);
		return (value != null) ? Boolean.parseBoolean(value) : null;
	}
	
	public Boolean getBoolean(String key, Boolean defaultValue) {
		String value = get(key);
		return (value != null) ? Boolean.parseBoolean(value) : defaultValue;
	}
	
	public boolean containsKey(String key) {
		return properties.containsKey(key);
	}
	
	public void set(String key,String value){
		properties.setProperty(key, value);
	}
	
}
